// CameraWindowCtrl.cpp : implementation file
//

#include "stdafx.h"
#include "DesktopClient.h"
#include "CameraWindowCtrl.h"
#include "DialogAddDevice.h"

#define		UC_BUTTON_PREVIOUS		WM_USER + 1031
#define		UC_BUTTON_NEXT			WM_USER + 1032

// CCameraWindowCtrl

IMPLEMENT_DYNAMIC(CCameraWindowCtrl, CWnd)

CCameraWindowCtrl::CCameraWindowCtrl()
{
	nChannelCnt = 0;

	for (int i = 0; i < MAX_CHANNEL_COUNT; i ++)
	{
		CameraWindow[i] = NULL;
		pCameraInfo[i] = NULL;
	}
}

CCameraWindowCtrl::~CCameraWindowCtrl()
{
	for (int i = 0; i < MAX_CHANNEL_COUNT; i ++)
	{
		if (CameraWindow[i])
		{
			if (IsWindow(CameraWindow[i]->m_hWnd))
				CameraWindow[i]->DestroyWindow();
			delete CameraWindow[i];
			delete pCameraInfo[i];
		}
	}
}


BEGIN_MESSAGE_MAP(CCameraWindowCtrl, CWnd)
	ON_WM_CREATE()
END_MESSAGE_MAP()



// CCameraWindowCtrl message handlers

int CCameraWindowCtrl::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CWnd::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	if (nChannelCnt < 3)
		nChannelCnt = 3;

	for (int i = 0; i < nChannelCnt; i ++)
	{
		if (CameraWindow[i]) 
		{
			if (IsWindow(CameraWindow[i]->m_hWnd))
				CameraWindow[i]->DestroyWindow();
			delete CameraWindow[i];
			delete pCameraInfo[i];
		}
		CameraWindow[i] = new CCameraWindow();
		if (CameraWindow[i]->Create(NULL, _T("CCameraWindow"), WS_CHILD | WS_VISIBLE | WS_BORDER, CRect(0, 0, 300, 300), this, WM_USER + 0x0002, NULL))
		{
			if (i < 3)
			{
				CameraWindow[i]->SetWindowPos(NULL, 20 + i * 320 + (i + 1) * 6, 5, 320, 420, SWP_SHOWWINDOW);
			}
			else
			{
				CameraWindow[i]->ShowWindow(FALSE);
			}

			if (pCameraInfo[i] == NULL)
			{
				pCameraInfo[i] = new CCameraInfo();				
			}
			CameraWindow[i]->pCameraInfo = pCameraInfo[i];
			CameraWindow[i]->nOwnerIndex = i;
		}
		else 
		{
			delete CameraWindow[i];
			CameraWindow[i] = NULL;
			continue;
		}
	}
	CRect rcClient;
	this->GetWindowRect(&rcClient);

	// Previous Channel Button
	btnPrev = new CClientButton();
	btnPrev->Create(_T("<"), WS_CHILD | WS_VISIBLE, CRect(0, 0, 0, 0), this, WM_USER + 0x0100);
	//btnPrev->SetIcon(IDR_MAINFRAME);
	btnPrev->OffsetColor(CClientButton::BTNST_COLOR_BK_IN, 30);
	btnPrev->SetColor(CClientButton::BTNST_COLOR_FG_IN, RGB(0, 128, 0));
	btnPrev->SetAlign(CClientButton::ST_ALIGN_HORIZ);
	btnPrev->m_nActionType = CClientButton::BTNST_ACTION_PREV_CHANNEL;
	// Next Channel Button
	btnNext = new CClientButton();
	btnNext->Create(_T(">"), WS_CHILD | WS_VISIBLE, CRect(0, 0, 0, 0), this, WM_USER + 0x0101);
	btnNext->OffsetColor(CClientButton::BTNST_COLOR_BK_IN, 30);
	btnNext->SetColor(CClientButton::BTNST_COLOR_FG_IN, RGB(0, 128, 0));
	btnNext->SetAlign(CClientButton::ST_ALIGN_HORIZ);
	btnNext->m_nActionType = CClientButton::BTNST_ACTION_NEXT_CHANNEL;

	btnNext->SetWindowPos(NULL, 1004, 0, 20, rcClient.Height(), SWP_SHOWWINDOW);
	btnPrev->SetWindowPos(NULL, 0, 0, 20, rcClient.Height(), SWP_SHOWWINDOW);

	return 0;
}

BOOL CCameraWindowCtrl::PreTranslateMessage(MSG* pMsg)
{
	switch (pMsg->message)
	{
	case WM_NOTIFY_BUTTON_CLICK:
		if (pMsg->wParam == WM_LBUTTONDOWN)
		{
			int *nActionType = (int*)pMsg->lParam;
			if (*nActionType == CClientButton::BTNST_ACTION_PREV_CHANNEL)
			{
				AfxMessageBox(_T("Previous Button clicked!"), MB_OK);
				return TRUE;
			}
			else if (*nActionType == CClientButton::BTNST_ACTION_NEXT_CHANNEL) 
			{
				OnBtnNextChannelClick();
				return TRUE;
			}
		}
		else if (pMsg->wParam == WM_REMOVE_CHANNEL) 
		{
			int* aIndex = (int*)pMsg->lParam;
			int iIndex = *aIndex;
			if (iIndex >= 0 && CameraWindow[iIndex])
			{
				if (IsWindow(CameraWindow[iIndex]->m_hWnd))
					CameraWindow[iIndex]->DestroyWindow();
				delete CameraWindow[iIndex];
				CameraWindow[iIndex] = NULL;
				nChannelCnt --;
				this->GetParent()->Invalidate();
				return TRUE;
			}
		} 
		else if (pMsg->wParam == WM_ADD_CHANNEL) 
		{
			int aIndex = *(int*)pMsg->lParam;
			CDialogAddDevice dlg;
			if (aIndex >= 0 && CameraWindow[aIndex])
			{
				dlg.pCameraInfo = CameraWindow[aIndex]->pCameraInfo;
				dlg.DoModal();
			} 
		}
		break;
	default:
		break;
	}

	return CWnd::PreTranslateMessage(pMsg);
}

void CCameraWindowCtrl::OnBtnNextChannelClick()
{
	for (int i = 0; i < nChannelCnt; i ++)
	{
		if (CameraWindow[i] && IsWindow(CameraWindow[i]->m_hWnd))
		{
			CRect rcWindow;
			CameraWindow[i]->GetWindowRect(&rcWindow);
			ScreenToClient(&rcWindow);
			CameraWindow[i]->MoveWindow(rcWindow.left + 5, rcWindow.top, rcWindow.Width(), rcWindow.Height());
			this->GetParent()->Invalidate(TRUE);
		}
	}
}

void CCameraWindowCtrl::OnBtnPrevChannelClick()
{

}